﻿using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.Areas.Admin.Controllers
{
    public class WebApiController : UnitController
    {
        /// <summary>
        /// Api核心事务
        /// </summary>
        private BaseApi _service = new BaseApi();

        #region 用户登录 （方法）
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="t">用户实体</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LoginJson(string username, string password, string code)
        {
            return _service.LoginJson(username, password, code);
        }
        #endregion
        #region 管理角色
        [HttpGet]
        public JsonResult ManagerRole(int id = 0)
        {
            return Json(_service.ManagerRoleInfo(id));
        }
        #endregion
        #region 获取所有站点
        public JsonResult siteJson()
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            var tree = _service.SiteTreeBind("is_lock=0", "sort_id asc,id desc");
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.Value, value = dr.Text });
            }
            return Json(list);
        }
        #endregion
        #region 获取对应站点频道
        public JsonResult channelJson(int id = 0)
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            string strwhere = string.Empty;
            if (id != 0)
            {
                strwhere = "site_id=" + id;
            }
            var tree = _service.ChannelTreeBind(strwhere, "sort_id asc,id asc");
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.Value, value = dr.Text });
            }
            return Json(list);
        }
        #endregion
        #region 获取对应站点频道栏目
        public JsonResult categoryJson(int id = 0)
        {
            IList<int> catlist;
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            var tree = _service.CategoryTreeBind(id, out catlist);
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.Value, value = dr.Text });
            }
            return Json(list);
        }
        public JsonResult categoryParent(int channel_id = 0)
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            var tree = new BLL.article_category().GetModelList(0, string.Format("channel_id={0} and parent_id=0", channel_id), "id,title", "sort_id asc,id desc");
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.id.ToString(), value = dr.title });
            }
            return Json(list);
        }
        public JsonResult categoryChild(int id, int channel_id = 0)
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            var tree = new BLL.article_category().GetModelList(0, string.Format("channel_id={0} and class_list like '%,{1},%' and parent_id>0", channel_id, id), "id,title", "sort_id asc,id desc");
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.id.ToString(), value = dr.title });
            }
            return Json(list);
        }
        #endregion
        #region 绑定站点Oauth平台
        public JsonResult oauthJson(int id = 0, int oauth_id = 0)
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            if (id > 0)
            {
                DataTable dt = _service.OauthBind(id, oauth_id);
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new Model.cascadejson { key = dr["id"].ToString(), value = dr["title"].ToString() });
                }
            }
            return Json(list);
        }
        #endregion
        #region 绑定公众账户
        public JsonResult weixinJson()
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            var tree = _service.WeixinTreeBind("", "sort_id asc,id desc");
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.Value, value = dr.Text });
            }
            return Json(list);
        }
        #endregion
        #region 绑定会员组别
        public JsonResult groupJson()
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            var tree = _service.GroupTreeBind("is_lock=0");
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.Value, value = dr.Text });
            }
            return Json(list);
        }
        #endregion
        #region 绑定配送方式
        public JsonResult expressJson()
        {
            List<Model.cascadejson> list = new List<Model.cascadejson>();
            var tree = _service.OrderExpressTreeBind();
            foreach (var dr in tree)
            {
                list.Add(new Model.cascadejson { key = dr.Value, value = dr.Text });
            }
            return Json(list);
        }
        #endregion
    }
}